﻿/* 
This file is part of Ingo Karstein's Web based PowerShell Console project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://webpsconsole.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace ik.PowerShell.WebPSConsole
{
    /// <summary>
    /// Summary description for OutputQueue
    /// </summary>
    public class OutputQueue : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            Server s = (Server)context.Session["server"];
            if( s == null )
                return;

            var cmd = context.Request["cmd"];
            if( string.IsNullOrEmpty(cmd) )
                return;

            if( string.Compare(cmd, "oc", true) == 0 )
            {
                context.Response.Write(s.OutputObjectCount.ToString());
                return;
            }

            if( string.Compare(cmd, "sn", true) == 0 )
            {
                context.Response.Write(Environment.MachineName);
                return;
            }

            if( string.Compare(cmd, "cu", true) == 0 )
            {
                context.Response.Write(System.Threading.Thread.CurrentPrincipal.Identity.Name);
                return;
            }

            if( string.Compare(cmd, "ir", true) == 0 )
            {
                context.Response.Write(( s.IsPowershellRunning ? 1 : 0 ).ToString());
                return;
            }

            if( string.Compare(cmd, "go", true) == 0 )
            {
                if( s.OutputObjectCount > 0 )
                {
                    OutputObject o = s.GetOutputObject();
                    context.Response.Write(o.ToJson());
                    if( o is OutputObjectEndSession )
                    {
                        context.Session.Abandon();
                    }
                }
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}